<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manage:education:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:education:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:education:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manage:education:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="educationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="员工基本信息主键id" align="center" prop="id" />-->
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="工号" align="center" prop="employeeNumber" />
      <el-table-column label="部门" align="center" prop="department">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.department_type" :value="scope.row.department"/>
        </template>
      </el-table-column>
      <el-table-column label="岗位" align="center" prop="position">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.post_type" :value="scope.row.position"/>
        </template>
      </el-table-column>
      <el-table-column label="工作性质" align="center" prop="workNature">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_nature" :value="scope.row.workNature"/>
        </template>
      </el-table-column>
      <el-table-column label="学历" align="center" prop="educationLevel" />
      <el-table-column label="毕业院校" align="center" prop="graduationSchool" />
      <el-table-column label="毕业专业" align="center" prop="graduationMajor" />
      <el-table-column label="教育结束时间" align="center" prop="educationEndDate" />
      <el-table-column label="学习形式" align="center" prop="studyForm" />
      <el-table-column label="毕业证书编号" align="center" prop="graduationCertificateNumber" />
      <el-table-column label="学位类型" align="center" prop="degreeType" />
      <el-table-column label="学位名称" align="center" prop="degreeName" />
      <el-table-column label="学位证书编号" align="center" prop="degreeCertificateNumber" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:education:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:education:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="工号" prop="employeeNumber">
          <el-input v-model="form.employeeNumber" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-select v-model="form.department" placeholder="请选择部门">
            <el-option
              v-for="dict in dict.type.department_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="岗位" prop="position">
          <el-select v-model="form.position" placeholder="请选择岗位">
            <el-option
              v-for="dict in dict.type.post_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作性质" prop="workNature">
          <el-select v-model="form.workNature" placeholder="请选择工作性质">
            <el-option
              v-for="dict in dict.type.work_nature"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">员工教育经历信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddEmployeesEducation">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteEmployeesEducation">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="employeesEducationList" :row-class-name="rowEmployeesEducationIndex" @selection-change="handleEmployeesEducationSelectionChange" ref="employeesEducation">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="学历" prop="educationLevel" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.educationLevel" placeholder="请输入学历" />
            </template>
          </el-table-column>
          <el-table-column label="毕业院校" prop="graduationSchool" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.graduationSchool" placeholder="请输入毕业院校" />
            </template>
          </el-table-column>
          <el-table-column label="毕业专业" prop="graduationMajor" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.graduationMajor" placeholder="请输入毕业专业" />
            </template>
          </el-table-column>
          <el-table-column label="教育结束时间" prop="educationEndDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.educationEndDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择教育结束时间" />
            </template>
          </el-table-column>
          <el-table-column label="学习形式" prop="studyForm" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.studyForm" placeholder="请输入学习形式" />
            </template>
          </el-table-column>
          <el-table-column label="毕业证书编号" prop="graduationCertificateNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.graduationCertificateNumber" placeholder="请输入毕业证书编号" />
            </template>
          </el-table-column>
          <el-table-column label="学位类型，如理学、工学、文学等" prop="degreeType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.degreeType" placeholder="请选择学位类型，如理学、工学、文学等">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="学位名称，如学士学位、硕士学位等" prop="degreeName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.degreeName" placeholder="请输入学位名称，如学士学位、硕士学位等" />
            </template>
          </el-table-column>
          <el-table-column label="学位证书编号" prop="degreeCertificateNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.degreeCertificateNumber" placeholder="请输入学位证书编号" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEducation, listEducationInfo,getEducation, delEducation, addEducation, updateEducation } from "@/api/manage/education";

export default {
  name: "Education",
  dicts: ['department_type', 'post_type', 'work_nature'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedEmployeesEducation: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工基本信息表格数据
      educationList: [],
      // 员工教育经历表格数据
      employeesEducationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        employeeNumber: [
          { required: true, message: "工号不能为空", trigger: "blur" }
        ],
        department: [
          { required: true, message: "部门不能为空", trigger: "change" }
        ],
        position: [
          { required: true, message: "岗位不能为空", trigger: "change" }
        ],
        workNature: [
          { required: true, message: "工作性质不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询员工基本信息列表 */
    getList() {
      this.loading = true;
      listEducationInfo(this.queryParams).then(response => {
        this.educationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        employeeNumber: null,
        department: null,
        position: null,
        workNature: null
      };
      this.employeesEducationList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEducation(id).then(response => {
        this.form = response.data;
        this.employeesEducationList = response.data.employeesEducationList;
        this.open = true;
        this.title = "修改员工基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.employeesEducationList = this.employeesEducationList;
          if (this.form.id != null) {
            updateEducation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEducation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除员工基本信息编号为"' + ids + '"的数据项？').then(function() {
        return delEducation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 员工教育经历序号 */
    rowEmployeesEducationIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 员工教育经历添加按钮操作 */
    handleAddEmployeesEducation() {
      let obj = {};
      obj.educationLevel = "";
      obj.graduationSchool = "";
      obj.graduationMajor = "";
      obj.educationEndDate = "";
      obj.studyForm = "";
      obj.graduationCertificateNumber = "";
      obj.degreeType = "";
      obj.degreeName = "";
      obj.degreeCertificateNumber = "";
      this.employeesEducationList.push(obj);
    },
    /** 员工教育经历删除按钮操作 */
    handleDeleteEmployeesEducation() {
      if (this.checkedEmployeesEducation.length == 0) {
        this.$modal.msgError("请先选择要删除的员工教育经历数据");
      } else {
        const employeesEducationList = this.employeesEducationList;
        const checkedEmployeesEducation = this.checkedEmployeesEducation;
        this.employeesEducationList = employeesEducationList.filter(function(item) {
          return checkedEmployeesEducation.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleEmployeesEducationSelectionChange(selection) {
      this.checkedEmployeesEducation = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manage/education/export', {
        ...this.queryParams
      }, `education_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
